package worldutils.huntgame.Utils;

import com.destroystokyo.paper.Title;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import worldutils.huntgame.HuntGame;

import java.util.UUID;

public class PlayerUtils {
    public static void sendMessageTosuicider(String message){
        sendMessageToTaggedPlayers(HuntGame.getMessage("runner"), message);
    }

    public static void sendMessageToProtector(String message){
        sendMessageToTaggedPlayers(HuntGame.getMessage("hunter"), message);
    }

    public static void sendMessageToAllPlayer(String message){
        Bukkit.broadcastMessage(message);
    }

    public static void sendMessageToTaggedPlayers(String tag, String message) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.sendMessage(ChatColor.YELLOW + message);
                }
            }
        }
    }

    public static void sendMessageToTaggedPlayersTitle(String tag, String message) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.sendTitle(new Title(message));
                }
            }
        }
    }

    public static void effectToTaggedPlayers(String tag, PotionEffect effect) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.addPotionEffect(effect);
                }
            }
        }
    }

    public static void cleareffectToTaggedPlayers(String tag) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.getActivePotionEffects().clear();
                }
            }
        }
    }

    public static void TeleportToTaggedPlayers(String tag, Location location) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.teleport(location);
                }
            }
        }
    }

    public static void setAllPlayersToSpectatorAndPlaySound() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            // 将玩家切换为旁观者模式
            player.setGameMode(GameMode.SPECTATOR);

            // 给玩家播放烟花音效
            player.playSound(player.getLocation(), Sound.ENTITY_FIREWORK_ROCKET_BLAST, 1.0f, 1.0f);
        }
    }

    public static void setAllPlayersToSc() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            player.setGameMode(GameMode.SURVIVAL);
        }
    }

    public static void SendCompass() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (TagUtils.getTag(player).equalsIgnoreCase(HuntGame.getMessage("hunter"))){
                ItemStack compass = new ItemStack(Material.COMPASS,1);
                player.getInventory().clear();
                player.getInventory().addItem(compass);
            }
        }
    }

    public static void SetEffect() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            ItemStack compass = new ItemStack(Material.COMPASS,1);
            player.setHealth(20);
            player.setFoodLevel(20);
        }
    }

    public static void setGamemodetoTag(String tag, GameMode gameMode) {
        for (UUID uuid : TagUtils.playerTags.keySet()) {
            if (TagUtils.playerTags.get(uuid).equalsIgnoreCase(tag)) {
                Player player = Bukkit.getPlayer(uuid);
                if (player != null && player.isOnline()) {
                    player.setGameMode(gameMode);
                }
            }
        }
    }
}
